package com.binance.notification.web.controller;

import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.notification.api.PushApi;
import com.binance.notification.api.push.notification.AndroidPayload;
import com.binance.notification.api.push.notification.AppPayload;
import com.binance.notification.api.push.notification.IosPayload;
import com.binance.notification.api.push.notification.MultipleNotificationPacket;
import com.binance.notification.api.push.notification.PcPayload;
import com.binance.notification.api.push.notification.SingleNotificationPacket;
import com.binance.notification.api.push.notification.WebPayload;
import com.binance.notification.web.push.Callback;
import com.binance.notification.web.service.IPushService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
public class PushController implements PushApi {

    @Resource
    private IPushService push;

    @Override
    public APIResponse<String> pushNotification(
            @Validated @RequestBody APIRequest<List<MultipleNotificationPacket>> request, @PathVariable boolean async) {
        String[] result = new String[1];
        if (!async) {
            this.push.sendMultiple(request.getBody(), new Callback(result));
        } else {
            this.push.sendMultiple(request.getBody(), null);
        }
        return APIResponse.getOKJsonResult(result[0]);
    }

    @Override
    public APIResponse<String> pushAppNotification(
            @Validated @RequestBody APIRequest<SingleNotificationPacket<AppPayload>> request,
            @PathVariable boolean async) {
        String[] result = new String[1];
        if (!async) {
            this.push.sendSingle(Arrays.asList(request.getBody()), new Callback(result));
        } else {
            this.push.sendSingle(Arrays.asList(request.getBody()), null);
        }
        return APIResponse.getOKJsonResult(result[0]);
    }

    @Override
    public APIResponse<String> pushAndroidNotification(
            @Validated @RequestBody APIRequest<SingleNotificationPacket<AndroidPayload>> request,
            @PathVariable boolean async) {
        String[] result = new String[1];
        if (!async) {
            this.push.sendSingle(Arrays.asList(request.getBody()), new Callback(result));
        } else {
            this.push.sendSingle(Arrays.asList(request.getBody()), null);
        }
        return APIResponse.getOKJsonResult(result[0]);
    }

    @Override
    public APIResponse<String> pushIosNotification(
            @Validated @RequestBody APIRequest<SingleNotificationPacket<IosPayload>> request,
            @PathVariable boolean async) {
        String[] result = new String[1];
        if (!async) {
            this.push.sendSingle(Arrays.asList(request.getBody()), new Callback(result));
        } else {
            this.push.sendSingle(Arrays.asList(request.getBody()), null);
        }
        return APIResponse.getOKJsonResult(result[0]);
    }

    @Override
    public APIResponse<String> pushPcNotification(
            @Validated @RequestBody APIRequest<SingleNotificationPacket<PcPayload>> request,
            @PathVariable boolean async) {
        String[] result = new String[1];
        if (!async) {
            this.push.sendSingle(Arrays.asList(request.getBody()), new Callback(result));
        } else {
            this.push.sendSingle(Arrays.asList(request.getBody()), null);
        }
        return APIResponse.getOKJsonResult(result[0]);
    }

    @Override
    public APIResponse<String> pushWebNotification(
            @Validated @RequestBody APIRequest<SingleNotificationPacket<WebPayload>> request,
            @PathVariable boolean async) {
        String[] result = new String[1];
        if (!async) {
            this.push.sendSingle(Arrays.asList(request.getBody()), new Callback(result));
        } else {
            this.push.sendSingle(Arrays.asList(request.getBody()), null);
        }
        return APIResponse.getOKJsonResult(result[0]);
    }

}
